package speedradar;

/**
 * Easiest Level 500 algorithm.
 * @author Seabook
 *
 */
public class SpeedRadar {

	public static void main(String[] args) {
		SpeedRadar r = new SpeedRadar();
		System.out.println(r.averageSpeed(1, 50, new int[] { 45, 40, 50 }));
		System.out.println(r.averageSpeed(1, 50, new int[] { 42, 43, 44, 45,
				46, 47, 48, 49, 50, 51 }));
		System.out.println(r.averageSpeed(1, 50,
				new int[] { 42, 46, 48, 50, 52 }));
		System.out.println(r.averageSpeed(20, 60, new int[] { 25, 45, 45, 43,

		24, 9, 51, 55, 60, 34, 61, 23, 40, 40, 47, 49, 33, 23, 47, 54, 54 }));
	}

	public double averageSpeed(int minLimit, int maxLimit, int[] readings) {
		double infringementCount = 0;
		double speedSum = 0;
		for (int i = 0; i < readings.length; i++) {
			int speed = readings[i];
			if (speed < minLimit || speed > maxLimit) {
				infringementCount++;
				continue;
			}
			speedSum += speed;
		}

		if (infringementCount / readings.length > 0.1) {
			return 0.0;
		}

		return speedSum / (readings.length - infringementCount);
	}
}
